<template>
  <div class="disease-statistics">
    <el-card class="page-container">
      <template #header>
        <div class="card-header">
          <span>按病种统计</span>
          <el-button type="success" @click="exportExcel" class="export-btn">导出统计结果</el-button>
        </div>
      </template>
      <!-- 筛选表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="病种">
          <el-select v-model="searchForm.disease" placeholder="请选择病种" clearable filterable>
            <el-option v-for="item in diseaseOptions" :key="item.code" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 统计表格 -->
      <el-table :data="filteredData" border style="width: 100%; margin-bottom: 30px;">
        <el-table-column prop="disease" label="病种名称" min-width="120" />
        <el-table-column prop="count" label="患者人数" min-width="100" />
        <el-table-column prop="totalAmount" label="报销总金额（元）" min-width="150" />
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

// 模拟病种选项
const diseaseOptions = [
  { code: 'F29  01', pinyin: 'jsb', name: '精神病' },
  { code: 'K74.151', pinyin: 'gyh', name: '肝硬化' },
  { code: 'I10  05', pinyin: 'gxys', name: '高血压Ⅲ' },
  { code: 'G20  02', pinyin: 'pjssb', name: '帕金森氏病' },
  { code: 'M32.901', pinyin: 'xtxhblc', name: '系统性红斑狼疮 NOS' },
  { code: 'N04.903', pinyin: 'sbzhz', name: '肾病综合征' },
  { code: 'M06.991', pinyin: 'lfsxgjy', name: '类风湿性关节炎 NOS' },
  { code: 'C00-C97>', pinyin: 'exzl', name: '恶性肿瘤' },
  { code: 'A15.001', pinyin: 'fjhxwjjzs', name: '肺结核，显微镜检证实' },
  { code: 'I10  04', pinyin: 'gxye', name: '高血压Ⅱ' },
  { code: 'I09.901', pinyin: 'fsxxzb', name: '风湿性心脏病(RHD)' },
  { code: 'I25.101', pinyin: 'gxb', name: '冠心病' },
  { code: 'N03.903', pinyin: 'mxsy', name: '慢性肾炎' },
  { code: 'E14.901', pinyin: 'tnb', name: '糖尿病 NOS' },
  { code: 'E05.901', pinyin: 'jzxjnkj', name: '甲状腺机能亢进' },
  { code: 'D61.905', pinyin: 'zszaxpx', name: '再生障碍性贫血 NOS' },
  { code: 'D56.001', pinyin: 'xdzhpx', name: 'α型地中海贫血' },
  { code: 'D66  02', pinyin: 'xyb', name: '血友病' },
  { code: 'zmqsb', pinyin: 'zmqsb', name: '终末期肾病（尿毒症）' },
]

// 筛选表单
const searchForm = reactive({
  disease: ''
})

// 模拟病种统计数据
const allData = ref([
  { year: 2022, disease: '高血压', count: 120, totalAmount: 35000 },
  { year: 2022, disease: '糖尿病', count: 80, totalAmount: 26000 },
  { year: 2022, disease: '冠心病', count: 60, totalAmount: 21000 },
  { year: 2023, disease: '高血压', count: 140, totalAmount: 41000 },
  { year: 2023, disease: '糖尿病', count: 95, totalAmount: 32000 },
  { year: 2023, disease: '冠心病', count: 70, totalAmount: 25000 },
  { year: 2024, disease: '高血压', count: 110, totalAmount: 33000 },
  { year: 2024, disease: '糖尿病', count: 100, totalAmount: 35000 },
  { year: 2024, disease: '冠心病', count: 85, totalAmount: 29000 },
])

// 过滤后的数据
const filteredData = computed(() => {
  if (!searchForm.disease) return allData.value
  return allData.value.filter(item => item.disease === searchForm.disease)
})

// 查询
const handleSearch = () => {
  ElMessage.success('筛选成功')
}
const resetSearch = () => {
  searchForm.disease = ''
}

// 导出功能
const exportExcel = () => {
  // 生成表头和数据
  const header = ['病种名称', '患者人数', '报销总金额（元）']
  const rows = filteredData.value.map(item => [item.disease, item.count, item.totalAmount])
  let csvContent = '\uFEFF' + header.join(',') + '\n' + rows.map(e => e.join(',')).join('\n')

  // 创建下载链接
  const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' })
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = `按病种统计.csv`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  ElMessage.success('导出成功')
}
</script>

<style scoped>
.disease-statistics {
  padding: 20px;
}
.page-container {
  min-height: calc(100vh - 140px);
}
.card-header {
  font-size: 18px;
  font-weight: bold;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.search-form {
  margin-bottom: 20px;
}
.export-btn {
  margin-left: auto;
}
</style>